package qnb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import qnb._common.easyui.EasyuiGridData;
import qnb._common.easyui.EasyuiPageVO;
import qnb._common.tools.StringTool;
import qnb.dao.NafeMapper;
import qnb.dao.SchoolRollMapper;
import qnb.entity.Nafe;
import qnb.entity.NafeExample;
import qnb.entity.SchoolRoll;
import qnb.entity.SchoolRollExample;
import qnb.service.SchoolRollService;

import java.util.List;

/**
 * @author huangxin
 * @date 2019/5/16-14:48
 */
@Service
public class SchoolRollServiceImpl implements SchoolRollService {
    @Autowired
    private SchoolRollMapper schoolRollMapper;

    @Override
    public EasyuiGridData listByPage(SchoolRoll schoolRoll, EasyuiPageVO vo) {
        SchoolRollExample example = new SchoolRollExample();
        SchoolRollExample.Criteria criteria = example.createCriteria();
        //判断搜索属性
        if (StringUtils.isNotBlank(schoolRoll.getSchoolRollName())) {
            criteria.andSchoolRollNameLike("%" + schoolRoll.getSchoolRollName() + "%");
        }

        if (schoolRoll.getSchoolRollStyle() != null) {
            criteria.andSchoolRollStyleEqualTo(schoolRoll.getSchoolRollStyle());
        }

        if (schoolRoll.getStudentId() != null) {
            criteria.andStudentIdEqualTo(schoolRoll.getStudentId());
        }

        //设置排序
        if (StringUtils.isNotBlank(vo.getSort())) {
            String sort = StringTool.formatStringNameToSqlName(vo.getSort());
            example.setOrderByClause(sort + " " + vo.getOrder());
        }

        //分页
        if (vo.getRows() > 0) {
            PageHelper.startPage(vo.getPage(), vo.getRows());
        }

        //查询
        List<SchoolRoll> list = schoolRollMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
//            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //解析分页结果
        PageInfo<SchoolRoll> info = new PageInfo<>(list);
        return EasyuiGridData.getEasyuiGridData((int) info.getTotal(), list);
    }

    @Override
    public int add(SchoolRoll schoolRoll) {
        return schoolRollMapper.insertSelective(schoolRoll);
    }

    @Override
    public int update(SchoolRoll schoolRoll) {
        return schoolRollMapper.updateByPrimaryKeySelective(schoolRoll);
    }

    @Override
    public int delete(Integer id) {
        return schoolRollMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SchoolRoll get(Integer id) {
        return schoolRollMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return schoolRollMapper.deleteByIds(ids);
    }


}
